package cn.cdutetc.foxwatch.controller;

import cn.cdutetc.foxwatch.exception.MyException;
import cn.cdutetc.foxwatch.pojo.Result;
import cn.cdutetc.foxwatch.pojo.UserPojo;
import cn.cdutetc.foxwatch.serveice.UserService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 王勇
 * @ClassName： loginController
 * @ClassPath： cn.cdutetc.foxwatch.controller.loginController
 * @Description：
 * @date 2024/10/8 20:16
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<String> register(@RequestBody UserPojo userPojo, @RequestParam("rePassword") String rePassword) throws Exception {
        System.out.println(userPojo.toString());
        Boolean i = userService.register(userPojo, rePassword);
        return i ? Result.success() : Result.error("注册失败");
    }

    @PostMapping("/login")
    public Result<Map<String, String>> login(@RequestBody UserPojo userPojo) throws MyException {
        System.out.println(userPojo.toString());
        String token = userService.login(userPojo);
        Map<String, String> result = new HashMap<>();
        result.put("token", token);
        return StringUtils.isEmpty(token) ? Result.error("登录失败") : Result.success(result);
    }
}
